package com.example.zhunongzonghe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.zhunongzonghe.common.Result;
import com.example.zhunongzonghe.entity.ZnFarmproduce;
import com.example.zhunongzonghe.entity.ZnLogistics;
import com.example.zhunongzonghe.service.ZnLogisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ZnLogistics")
public class ZnLogisticsController {
    @Autowired
    private ZnLogisticsService znLogisticsService;

    @GetMapping("/ZnLogisticsAll")
    public Result QueryLogisticsAll(){
        List<ZnLogistics> znSortList = znLogisticsService.list();
        return new Result<>(true,"查询成功",200,znSortList);
    }

    @PostMapping("/addLogistics")
    public Result addLogistics(@RequestBody ZnLogistics znLogistics){
        if (znLogistics==null) return new Result<>(false,"数据异常",400);
        znLogisticsService.save(znLogistics);
        return new Result<>(true,"添加成功",200);
    }
    @PostMapping("/updateLogistics")
    public Result UpdateLogistics(@RequestBody ZnLogistics znLogistics){
        if (znLogistics==null) return new Result<>(false,"数据异常",400);
        znLogisticsService.updateById(znLogistics);
        return new Result<>(true,"添加成功",200);
    }

    @DeleteMapping("/deleteLogistics/{id}")
    public Result deleteLogistics(@PathVariable Integer id){
        if (id==null) return new Result<>(false,"数据异常",400);
        LambdaQueryWrapper<ZnLogistics> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnLogistics::getId,id);
        LambdaQueryWrapper<ZnFarmproduce> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ZnFarmproduce::getSortId,id);
        znLogisticsService.remove(queryWrapper);
        return new Result<>(true,"删除成功",200);
    }
}
